<?php

App::uses('AppController', 'Controller');
App::uses('Account', 'Model');

class TransactionsController extends AppController {

	public $uses = array('Transaction', 'Account');

	public function index($account_id = null, $category_id = null, $from = null, $to = null) {
		$this->log(__METHOD__, 'debug');
	}

	public function ajax_index() {
		$this->log(__METHOD__, 'debug');
		if ($this->request->is('Ajax')) {
			$this->autoLayout = false;
			$this->autoRender = false;
			
			$this->log('data: ' . var_export($this->request->data, true), 'debug');

			$account_id = $this->request->data['account_id'];
			$category_id = $this->request->data['category_id'];
			$base_date = !empty($this->request->data['base_date']) ? strtotime($this->request->data['base_date']) : strtotime('now');
			$modifier = !empty($this->request->data['modifier']) ? $this->request->data['modifier'] : 0;
			$view_mode = !empty($this->request->data['view_mode']) ? $this->request->data['view_mode'] : 'week';

			$new_base_date = ('week' == $view_mode) ? strtotime($modifier . ' week', $base_date) : strtotime($modifier . ' month', $base_date);
			
			//ISO-8601 numeric representation of the day of the week. 1 (for Monday) through 7 (for Sunday)
			$weekday = date('N', $base_date);
			$week_start = strtotime(($weekday - 1) . ' day ago', $new_base_date);
			$week_end = strtotime((7 - $weekday) . ' day', $new_base_date);
			$month_start = strtotime('first day of this month', $new_base_date);
			$month_end = strtotime('last day of this month', $new_base_date);
			
			$this->log('$new_base_date: ' . $new_base_date, 'debug');
			$this->log('$week_start: ' . date('Y-m-d', $week_start), 'debug');
			$this->log('$week_end: ' . date('Y-m-d', $week_end), 'debug');
			$this->log('$month_start: ' . date('Y-m-d', $month_start), 'debug');
			$this->log('$month_end: ' . date('Y-m-d', $month_end), 'debug');

			$from = ('week' == $view_mode) ? date('Y-m-d 00:00:00', $week_start) : date('Y-m-d 00:00:00', $month_start);
			$to = ('week' == $view_mode) ? date('Y-m-d 00:00:00', $week_end) : date('Y-m-d 00:00:00', $month_end);

//			$from = date('Y-m-d 00:00:00', $from);
//			$to = date('Y-m-d 23:59:59', $to);
			
			$this->log('$from: ' . $from, 'debug');
			$this->log('$to: ' . $to, 'debug');

			$conditions = array();
			if (!empty($account_id)) {
				$conditions['account_id'] = $account_id;
			}
			if (!empty($category_id)) {
				$conditions['category_id'] = $category_id;
			}
			$conditions['date >='] = $from;
			$conditions['date <='] = $to;
			$this->log('$conditions: ' . var_export($conditions, true), 'debug');

			$transactions = $this->Transaction->find('all', array(
				'conditions' => $conditions,
				'order' => array('Transaction.date' => 'desc', 'Transaction.id' => 'desc')
			));
//			$this->log('$transactions: ' . var_export($transactions, true), 'debug');

			//For next request
			$base_date = $from;
			
			$this->set(compact('view_mode', 'base_date', 'from', 'to', 'transactions'));
			$this->render('../Elements/Transactions/index');
			return;
		}
	}

	public function edit($id = null) {
		if ($this->request->is('post') || $this->request->is('put')) {
			$data = $this->request->data['Transaction'];
			$this->log($data, 'debug');
            
            try {
				if (empty($id)) {
					$this->Transaction->create();
				} else {
					$this->Transaction->id = $id;
				}
				$transaction = $this->Transaction->save($data);

				//Save account balance
				if (false !== $transaction) {
					$type = strtolower($data['type']);
					if (in_array($type, Account::$sumary_amounts)) {
						$db = $this->Account->getDataSource();
						$this->Account->save(array(
							'id' => $data['account_id'],
							$type => $db->expression($type . ' + ' . $data['amount']),
						));
					}
				}
			} catch (Exception $e) {
				$transaction = false;
				$this->log($e->getMessage(), 'error');
			}

			if ($this->request->is('Ajax')) {
				//Ajax request
				$this->autoRender = false;
				$this->log(__METHOD__ . ' called via Ajax.', 'debug');
				if (false === $transaction) {
					return json_encode(array('status' => false, 'message' => 'Thất bại'));
				}
				return json_encode(array('status' => true, 'message' => 'Đã lưu thành công.', 'id' => $this->Transaction->id));
			} else {
				//Normal post
				if ($transaction) {
					$this->Session->setFlash('Transaction saved');
				} else {
					$this->Session->setFlash('Transaction not saved');
				}
			}
		}
	}

}
